<?php
//核心数据库操作 使用pdo方式
class PdoSql
{
	// 声明$instance为私有静态类型，用于保存当前类实例化后的对象
    private static $instance = null;
	// 数据库连接句柄
	private $db =null;

    public $filter = '';

    public $where = '';

    public $params = [];
	 // 连接数据库
    private function __construct($host, $username, $password, $dbname)
    {
    	
        try {
            $dsn = sprintf("mysql:host=%s;dbname=%s;charset=utf8", $host, $dbname);
            $option = array(
            	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
			);
            $this->db = new PDO($dsn, $username, $password, $option);

        } catch (PDOException $e) {
            exit('错误: ' . $e->getMessage());
        }
    }
		
	 // 获取数据库句柄方法
    public function db()
    {
        return $this->db;
    }	
	// 这是获取当前类对象的唯一方式
	public static function getInstance($config = array())
    {
        // 检查对象是否已经存在，不存在则实例化后保存到$instance属性
        if(self::$instance == null) {
            self::$instance = new self($config['host'],$config['username'],$config['password'],$config['dbname']);

        }
        return self::$instance;
    }


    //更新
    public function updateById($goods_id,$update_array = array(),$where = array())
    {
        if(!empty($update_array))
        {
            $sql = "update ".$this->_table." set ";
            foreach ($update_array as $key => $value)
            {
                $sql .= "`".$key."`='".$value."',";
            }
            $sql = rtrim($sql,",");
            $sql .= " where id = ".$goods_id;

            if(!empty($where))
            {
                $and ="";
                foreach ($where as $key => $value)
                {
                    $and .= " and ".$key." = '".$value."'";
                }
                $sql .= $and;
            }

            return self::getInstance()->db()->exec($sql);
        }else
        {
            return null;
        }
    }

	// 声明成私有方法，禁止克隆对象
    private function __clone(){}
    // 声明成私有方法，禁止重建对象
    private function __wakeup(){}


    public function beginTransaction()
    {
        return self::getInstance()->db()->beginTransaction();
    }

    public function commit()
    {
        return self::getInstance()->db()->commit();
    }

    public function rollBack()
    {
        return self::getInstance()->db()->rollBack();
    }
	
	public function findAll()
	{
		$sql = sprintf("select * from `%s`", $this->_table);
        $sth = self::getInstance()->db()->prepare($sql);
        $sth->execute();
        $result = $sth->fetchAll();
        if($result)
        {
            return $result;
        }else{
            return null;
        }

	}

    /**
     * 析构函数
     * 释放结果集和销毁变量
     */
    public function __destruct()
    {

    }

    public function findRow()
    {
        $sql = sprintf("select * from `%s` limit 1",$this->_table);
        $sth = self::getInstance()->db()->prepare($sql);
        $sth->execute();
        $result = $sth->fetch();
        if($result)
        {
            return $result;
        }else
        {
            return null;
        }
    }

    public function addParams($params)
    {
        if (!empty($params)) {
            if (empty($this->params)) {
                $this->params = $params;
            } else {
                foreach ($params as $name => $value) {
                    if (is_int($name)) {
                        $this->params[] = $value;
                    } else {
                        $this->params[$name] = $value;
                    }
                }
            }
        }
        return $this;
    }
	
	 // 查询条件
	
	public function where($condition,$params = array())
	{
        $this->where = $condition;
        $this->addParams($params);
        return $this;
	}
	
	public function one()
	{
		
	}
	
}
?>